package idv.arthur.work;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet implementation class DelMRitem
 */ 
public class IsDuplicated extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public IsDuplicated() {
        super();
        // TODO Auto-generated constructor stub
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	doPost(request,response);
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		
		Widgets wid = new Widgets();
		Connection conn = wid.getConn(true);
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String strSQL = "SELECT COUNT(*) FROM env WHERE bssid=? AND subsysid=? AND envid=?";
		
		String bssId = request.getParameter("bssId_form");
		String subSysId = request.getParameter("subSysId_form");
		String envId = request.getParameter("envId_form");
		String res = "";
		
		try {
			ps = conn.prepareStatement(strSQL);
			ps.setString(1, bssId);
			ps.setString(2, subSysId);
			ps.setString(3, envId);
			rs = ps.executeQuery();
			rs.next();
			res = String.valueOf(rs.getInt(1)==0?false:true );
		} catch (SQLException e) {
			res = wid.errProcess(e);
		} finally {
			try {
				if (rs!= null) {rs.close();}
				if (ps!= null) {ps.close();}
				if (conn!= null) {conn.close();}
			} catch (SQLException e) {
				res = wid.errProcess(e);
			}
		}
		
		response.setContentType("application/text");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(res);		
	}
}